<?php 

include '../function.php';

$dsn = "mysql:host=localhost;dbname=school";
$username="root";
$password = "";

try{

	$pdo = new Pdo($dsn,$username,$password);

}catch(PDOException $e) {

	die($e->getMessage());

}

$pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);

// 设置统一的字符集
$pdo->query("SET NAMES utf8");

// 执行查询,返回结果集对象不成功
$result = $pdo->query("SELECT * from stu");

if($result){
	// var_dump($result);
	// 返回一条数据
	var_dump($result->fetch());

	// 返回关联数组的所有数据
	var_dump($result->fetchAll(PDO::FETCH_ASSOC));
}


// 执行删除，返回删除成功或者失败

try {
	// 组合sql
	$sql = "delete from stu where id>20";
	//执行sql
	$affected_rows = $pdo->exec($sql);
	echo "共删除成功{$affected_rows}条记录";
} catch (PDOException $e) {
	// 抛出异常
	echo $e->getMessage();
}
















 ?>